package cn.display.pixelworksx7.ui.main

// import cn.display.pixelworksx7.service.FloatWindowService

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import cn.display.pixelworksx7.databinding.OtherSettingsBinding
import cn.display.pixelworksx7.manager.FloatWindowManager
import cn.display.pixelworksx7.util.DateUtil
import cn.display.pixelworksx7.util.ShellUtil
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
import java.util.*


class OtherSettingsFragment : Fragment() {
    private var _binding: OtherSettingsBinding? = null
    private val binding get() = _binding!!
    private val TAG = "OtherSettingsFragment";

     private lateinit var floatWindowManager: FloatWindowManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = OtherSettingsBinding.inflate(inflater, container, false)
        val root = binding.root
        var disableCpufreqLimitExists = File("/proc/game_opt/disable_cpufreq_limit").exists()
        if (disableCpufreqLimitExists) {
            binding.cpufreqLimit.setOnClickListener {
                var result = ShellUtil.execCommand("cat /proc/game_opt/disable_cpufreq_limit", true);
                if (result.successMsg.equals("1")) {
                    Thread.sleep(100)
                    ShellUtil.toast(context, "开启限制cpu频率")
                    ShellUtil.execCommand("echo 0 >/proc/game_opt/disable_cpufreq_limit", true)
                } else if (result.successMsg.equals("0")) {
                    Thread.sleep(100)
                    ShellUtil.toast(context, "禁用限制cpu频率")
                    ShellUtil.execCommand("echo 1 >/proc/game_opt/disable_cpufreq_limit", true)
                }
            }
        } else {
            binding.cpufreqLimit.visibility = View.GONE
        }
        binding.writeCurrentDate.setOnClickListener {
            ShellUtil.setTextToClipboard(this.context, DateUtil.formatDate(Date()))
        }
        binding.showFloatWindow.setOnClickListener {
            floatWindowManager = FloatWindowManager(requireContext())
        // 创建悬浮窗布局
            val layoutInflater = LayoutInflater.from(requireContext())
            floatWindowManager.createFloatingWindow(layoutInflater)
            // 显示悬浮窗
            val windowManager = requireActivity().windowManager
            floatWindowManager.showFloatingWindow(windowManager)
        }
        return root
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): OtherSettingsFragment {
            return OtherSettingsFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
